package com.biostime.qdingding.modules.qd.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Activity;
import com.biostime.qdingding.modules.qd.entity.Album;
import com.biostime.qdingding.modules.qd.service.ActivityService;
import com.biostime.qdingding.modules.qd.service.AlbumService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 活动Controller
 * @author king
 * @version 2015-12-15
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/activity")
public class ActivityController extends BaseController {

	@Autowired
	private ActivityService activityService;
	@Autowired
	private AlbumService albumService;
	
	@ModelAttribute
	public Activity get(@RequestParam(required=false) String id) {
		Activity entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = activityService.get(id);
		}
		if (entity == null){
			entity = new Activity();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:activity:view")
	@RequestMapping(value = {"","list"})
	public String list(Activity activity, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Activity> page = activityService.findPage(new Page<Activity>(request, response), activity); 
		model.addAttribute("page", page);
		return "modules/qd/activityList";
	}

	@RequiresPermissions("qd:activity:view")
	@RequestMapping(value = "form")
	public String form(Activity activity, Model model) {
		
		if(activity.getId() != null && activity.getId() !=""){
			Album ca = new Album();
			ca.setCategory("activity");
			ca.setObjectId(StringUtils.toInteger(activity.getId()));
			List<Album> activityAlbum = albumService.findList(ca);
			model.addAttribute("activityAlbum", activityAlbum);
		}
		model.addAttribute("activity", activity);
		return "modules/qd/activityForm";
	}

	@RequiresPermissions(value={"qd:activity:edit","qd:activity:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Activity activity, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, activity)){
			return form(activity, model);
		}
		
		User curUser = UserUtils.getUser();
		
		activity.setCenterId(Integer.valueOf(curUser.getCenter().getId()));
		
		activityService.save(activity);
		addMessage(redirectAttributes, "保存成功");
		
		Album cla = new Album();
		cla.setCategory("activity");
		cla.setObjectId(StringUtils.toInteger(activity.getId()));
		albumService.deleteAll(cla);
		
		String[] photos = request.getParameterValues("hid_photo_name");
		String[] remarks = request.getParameterValues("hid_photo_remark");
		if(photos!=null){
			for(int i=0;i<photos.length;i++){
				String photo = photos[i];
				String remark = remarks[i];
				
				if(!StringUtils.isEmpty(photo)){
					String[] parts = photo.split("\\|");
					if(parts!=null && parts.length>=6){
						Album album = new Album();
						album.setObjectId(StringUtils.toInteger(activity.getId()));
						album.setCategory(parts[0]);
						album.setFileName(parts[1]);
						album.setOrginPath(parts[2]);
						album.setThumbPath(parts[3]);
						album.setAlbumSize(StringUtils.toInteger(parts[4]));
						album.setRemarks(remark);
						
						albumService.save(album);
					}
				}
			}
		}
		
		return "redirect:"+Global.getAdminPath()+"/qd/activity/?repage";
	}
	
	@RequiresPermissions("qd:activity:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Activity activity, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				activityService.delete(new Activity(id));
			}
		}else{
			activityService.delete(activity);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/activity/?repage";
	}

}